// @@@ START COPYRIGHT @@@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//
// @@@ END COPYRIGHT @@@
/* -*-java-*-
 * Filename    : TStatement.java
 * Description :
 *
 * --------------------------------------------------------------------
 */
package org.apache.trafodion.jdbc.t2;

import java.sql.*;
import java.lang.String;
import java.util.Date;
import java.util.Vector;
import java.io.PrintWriter;

import org.apache.trafodion.jdbc.t2.SQLMXResultSet;
import org.apache.trafodion.jdbc.t2.SQLMXStatement;

public class TStatement implements java.sql.Statement {
	// java.sql.Statement interface Methods
	public void addBatch(String sql) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "addBatch(\"" + sql + "\")");
		stmt_.addBatch(sql);
	}

	public void cancel() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "cancel()");
		stmt_.cancel();
	}

	public void clearBatch() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "clearBatch()");
		stmt_.clearBatch();
	}

	public void clearWarnings() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "clearWarnings()");
		stmt_.clearWarnings();
	}

	public void close() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "close()");
		stmt_.close();
	}

	public boolean execute(String sql) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "execute(\"" + sql + "\")");
		boolean retValue;

		retValue = stmt_.execute(sql);
		return retValue;
	}

	public int[] executeBatch() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "executeBatch()");
		int[] retValue;

		retValue = stmt_.executeBatch();
		return retValue;
	}

	public ResultSet executeQuery(String sql) throws SQLException {
		ResultSet resultSet;

		if (out_ != null)
			out_.println(getTraceId() + "executeQuery(\"" + sql + "\")");

		resultSet = stmt_.executeQuery(sql);

		if (out_ != null)
			out_.println(getTraceId() + "executeQuery(\"" + sql
					+ "\") returns ResultSet ["
					+ System.identityHashCode(resultSet) + "]");

		if(resultSet instanceof SQLMXResultSet) {
			((SQLMXResultSet) resultSet).setTracer(out_);
			rs_ = resultSet;
		} else {
			rs_ = new TResultSet(resultSet, this, out_);
		}
		return rs_;
	}

	public int executeUpdate(String sql, int autoGeneratedKeys)
			throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "executeUpdate(\"" + sql + ", "
					+ autoGeneratedKeys + "\")");
		int retValue;

		retValue = stmt_.executeUpdate(sql, autoGeneratedKeys);
		return retValue;
	}

	public Connection getConnection() throws SQLException {
		Connection retValue;

		if (out_ != null)
			out_.println(getTraceId() + "getConnection()");

		retValue = stmt_.getConnection();

		if (out_ != null)
			out_.println(getTraceId() + "getConnection() returns Connection ["
					+ System.identityHashCode(retValue) + "]");

		return retValue;
	}

	public int getFetchDirection() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getFetchDirection()");
		int retValue;

		retValue = stmt_.getFetchDirection();
		return retValue;
	}

	public int getFetchSize() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getFetchSize()");
		int retValue;

		retValue = stmt_.getFetchSize();
		return retValue;
	}

	public ResultSet getGeneratedKeys() throws SQLException {
		ResultSet retValue;

		if (out_ != null)
			out_.println(getTraceId() + "getGeneratedKeys()");

		retValue = stmt_.getGeneratedKeys();

		if (out_ != null)
			out_.println(getTraceId() + "getGeneratedKeys() returns ResultSet ["
					+ System.identityHashCode(retValue) + "]");

		return retValue;
	}

	public int getMaxFieldSize() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getMaxFieldSize()");
		int retValue;

		retValue = stmt_.getMaxFieldSize();
		return retValue;
	}

	public int getMaxRows() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getMaxRows()");
		int retValue;

		retValue = stmt_.getMaxRows();
		return retValue;
	}

	public boolean getMoreResults() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getMoreResults()");
		boolean retValue;

		retValue = stmt_.getMoreResults();
		return retValue;
	}

	public boolean getMoreResults(int current) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getMoreResults(\"" + current + "\")");
		boolean retValue;

		retValue = stmt_.getMoreResults(current);
		return retValue;
	}

	public int getQueryTimeout() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getQueryTimeout()");
		int retValue;

		retValue = stmt_.getQueryTimeout();
		return retValue;
	}

	public ResultSet getResultSet() throws SQLException {
		ResultSet resultSet;

		if (out_ != null)
			out_.println(getTraceId() + "getResultSet()");

		resultSet = stmt_.getResultSet();

		if (out_ != null)
			out_.println(getTraceId() + "getResultSet() returns ResultSet ["
					+ System.identityHashCode(resultSet) + "]");

		if (rs_ != null)
			return rs_;
		else {
			if(resultSet instanceof SQLMXResultSet) {
				if(((SQLMXResultSet)resultSet).getTracer() == null) {
					((SQLMXResultSet)resultSet).setTracer(out_);
					rs_ = resultSet;
				}
			} else {
				rs_ = new TResultSet(resultSet, this, out_);
			}
			return rs_;
		}
	}

	public int getResultSetConcurrency() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getResultSetConcurrency()");
		int retValue;

		retValue = stmt_.getResultSetConcurrency();
		return retValue;
	}

	public int getResultSetType() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getResultSetType()");
		int retValue;

		retValue = stmt_.getResultSetType();
		return retValue;
	}

	public int getUpdateCount() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getUpdateCount()");
		int retValue;

		retValue = stmt_.getUpdateCount();
		return retValue;
	}

	public SQLWarning getWarnings() throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "getWarnings()");
		SQLWarning retValue;

		retValue = stmt_.getWarnings();
		return retValue;
	}

	public void setCursorName(String name) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "setCursorName(\"" + name + "\")");
		stmt_.setCursorName(name);
	}

	public void setEscapeProcessing(boolean enable) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "setEscapeProcessing(" + enable + ")");
		stmt_.setEscapeProcessing(enable);
	}

	public void setFetchDirection(int direction) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "setFetchDirection(" + direction + ")");
		stmt_.setFetchDirection(direction);
	}

	public void setFetchSize(int rows) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "setFetchSize(" + rows + ")");
		stmt_.setFetchSize(rows);
	}

	public void setMaxFieldSize(int max) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "setMaxFieldSize(" + max + ")");
		stmt_.setMaxFieldSize(max);
	}

	public void setMaxRows(int max) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "setMaxRows(" + max + ")");
		stmt_.setMaxRows(max);
	}

	public void setQueryTimeout(int seconds) throws SQLException {
		if (out_ != null)
			out_.println(getTraceId() + "setQueryTimeout(" + seconds + ")");
		stmt_.setQueryTimeout(seconds);
	}

	public boolean execute(String str, String[] str1) throws SQLException {
		boolean retValue;

		if (out_ != null) {
			out_.println(getTraceId() + "execute(\"" + str + "\"," + str1 + ")");
		}

		retValue = stmt_.execute(str, str1);
		return retValue;
	}

	public boolean execute(String str, int[] values) throws SQLException {
		boolean retValue;

		if (out_ != null) {
			out_.println(getTraceId() + "execute(\"" + str + "\"," + values + ")");
		}

		retValue = stmt_.execute(str, values);
		return retValue;
	}

	public boolean execute(String str, int param) throws SQLException {
		boolean retValue;

		if (out_ != null) {
			out_.println(getTraceId() + "execute(\"" + str + "\"," + param + ")");
		}

		retValue = stmt_.execute(str, param);
		return retValue;
	}

	public int executeUpdate(String str) throws SQLException {
		int retValue;

		if (out_ != null) {
			out_.println(getTraceId() + "executeUpdate(\"" + str + "\")");
		}

		retValue = stmt_.executeUpdate(str);
		return retValue;
	}

	public int executeUpdate(String str, String[] str1) throws SQLException {
		int retValue;

		if (out_ != null) {
			out_.println(getTraceId() + "executeUpdate(\"" + str + "\"," + str1
					+ ")");
		}

		retValue = stmt_.executeUpdate(str, str1);
		return retValue;
	}

	public int executeUpdate(String str, int[] values) throws SQLException {
		int retValue;

		if (out_ != null) {
			out_.println(getTraceId() + "executeUpdate(\"" + str + "\"," + values
					+ ")");
		}

		retValue = stmt_.executeUpdate(str, values);
		return retValue;
	}

	public int getResultSetHoldability() throws SQLException {
		int retValue;

		if (out_ != null) {
			out_.println(getTraceId() + "getResultSetHoldability()");
		}

		retValue = stmt_.getResultSetHoldability();
		return retValue;
	}

	public TStatement(Statement stmt, PrintWriter out) {	
		stmt_ = stmt;
		out_ = out;
		
	}

	TStatement() {
	}

	public void setTraceId(String traceId_) {
		this.traceId_ = traceId_;
	}

	public String getTraceId() {
		String className = null;
		String label=null;
		if (stmt_ != null){
			className = stmt_.getClass().getName();
			
			if(stmt_ instanceof SQLMXStatement){
			label=((SQLMXStatement)stmt_).getStmtLabel_();
			}
			else {
				label=((SQLMXStatement)(((TStatement)stmt_).stmt_)).getStmtLabel_();
			}

		// Build up jdbcTrace output entry
		// Build up jdbcTrace output entry
		setTraceId(org.apache.trafodion.jdbc.t2.T2Driver.traceText
				+ org.apache.trafodion.jdbc.t2.T2Driver.dateFormat.format(new Date())
				+ "]:["
				+ Thread.currentThread()
				+ "]:["
				+ label+ "]:"
				+ className.substring(
						org.apache.trafodion.jdbc.t2.T2Driver.REMOVE_PKG_NAME, className
								.length()) + ".");
		}
		return traceId_;
	}

	// Fields
	Statement stmt_;
	ResultSet rs_;
	private String traceId_;
	PrintWriter out_;

        public Object unwrap(Class iface) throws SQLException {
                // TODO Auto-generated method stub
                return null;
        }

        public boolean isWrapperFor(Class iface) throws SQLException {
                // TODO Auto-generated method stub
                return false;
        }

        public boolean isClosed() throws SQLException {
                // TODO Auto-generated method stub
                return false;
        }

        public void setPoolable(boolean poolable) throws SQLException {
                // TODO Auto-generated method stub

        }
        public boolean isPoolable() throws SQLException {
                // TODO Auto-generated method stub
                return false;
        }

        public void closeOnCompletion() throws SQLException {
                // TODO Auto-generated method stub

        }

        public boolean isCloseOnCompletion() throws SQLException {
                // TODO Auto-generated method stub
                return false;
        }

}
